using System;
using System.IO;
using UnityEngine;
/// <summary>
/// 资源路径属性
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class HFAssetBundlePathAttribute : PropertyAttribute
{
    /// <summary>
    /// 资源Key
    /// </summary>
    public int assetBundleKey { get; private set; }

    /// <summary>
    /// 资源名称
    /// </summary>
    public string assetName { get; private set; }

    /// <summary>
    /// AssetBundle资源路径
    /// </summary>
    public string assetBundlePath { get; private set; }

    /// <summary>
    /// Editor编辑器路径
    /// </summary>
    public string editorPath { get; private set; }

    /// <summary>
    /// 线程优先级
    /// </summary>
    public ThreadPriority threadPriority { get; private set; }

    /// <summary>
    /// 根路径
    /// </summary>
    public enHFLanguageRootFolder rootFolder { get; private set; }

    /// <summary>
    /// 当前语言
    /// </summary>
    public enHFLanguageClassify language { get; private set; }

    /// <summary>
    /// 相对根路径
    /// </summary>
    public string relativeRootPath { get; private set; }

    /// <summary>
    /// 是否是场景
    /// </summary>
    public bool isScene { get; private set; }

    /// <summary>
    /// 是否忽略语言包
    /// </summary>
    public bool isIngoreLanguage { get; private set; }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="_rootPath">资源根路径</param>
    /// <param name="_relativeRootPath">相对_rootPath的路径</param>
    /// <param name="_isIngoreLanguage">是否忽略语言包</param>
    /// <param name="_backgroundLoadingPriority">线程优先级</param>
    public HFAssetBundlePathAttribute(enHFLanguageRootFolder _rootPath, string _relativeRootPath, bool _isIngoreLanguage = false,
        int _backgroundLoadingPriority = enHFThreadPriority.Normal)
    {
        threadPriority = (ThreadPriority)_backgroundLoadingPriority;
        rootFolder = _rootPath;
        relativeRootPath = _relativeRootPath;
        isIngoreLanguage = _isIngoreLanguage;
        string ext = Path.GetExtension(_relativeRootPath);
        isScene = enHFUnityFileExt.Scene.HFFirstAttribute<HFFileExtAttribute>().IsExt(ext);        
    }

    /// <summary>
    /// 变更语言包
    /// </summary>
    /// <param name="_lan">语言包</param>
    public void ChangeLanguage(enHFLanguageClassify _lan)
    {
        language = _lan;
        string relativePath = OnGetRelativeAssetPath(relativeRootPath, _lan);
        if (isIngoreLanguage)
        {
            editorPath = Path.Combine(HFLanguageHelper.GetRootFolder(rootFolder), relativePath).HFTransPathSeparatorCharToUnityChar();
        }
        else
        {
            editorPath = Path.Combine(HFLanguageHelper.GetAssetBundleRootPath(rootFolder, _lan), relativePath).HFTransPathSeparatorCharToUnityChar();
        }        
        assetBundlePath = editorPath + HFGlobalUtility.assetBundleNameSuffixExt.ext;
        assetBundleKey = assetBundlePath.HFAssetBundleKey();
        assetName = Path.GetFileNameWithoutExtension(editorPath);
    }

    /// <summary>
    /// 复制
    /// </summary>
    /// <returns>属性</returns>
    public HFAssetBundlePathAttribute Copy()
    {
        HFAssetBundlePathAttribute result = OnCopy(rootFolder, relativeRootPath, isIngoreLanguage, threadPriority);
        result.assetBundleKey = assetBundleKey;
        result.assetBundlePath = assetBundlePath;
        result.editorPath = editorPath;
        result.assetName = assetName;
        result.isScene = isScene;
        return result;
    }

    /// <summary>
    /// 复制
    /// </summary>
    /// <returns>复制后对象</returns>
    protected virtual HFAssetBundlePathAttribute OnCopy(enHFLanguageRootFolder _rootPath, string _relativeRootPath,bool _isIngoreLanguage, ThreadPriority _threadPriority)
    { return new HFAssetBundlePathAttribute(_rootPath, _relativeRootPath, isIngoreLanguage, (int)_threadPriority); }

    /// <summary>
    /// 获得相对资源路径
    /// </summary>
    /// <param name="_relativeRootPath">相对_rootPath的路径</param>
    /// <param name="_lan">语言包</param>
    /// <returns>相对资源路径</returns>
    protected virtual string OnGetRelativeAssetPath(string _relativeRootPath, enHFLanguageClassify _lan) { return _relativeRootPath; }
}
